package appfight.map;

import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.IModifier.IModifierListener;

import android.util.Log;
import appfight.level.GameLevel;

public class MapMainCharacter{
	
	public AnimatedSprite sprite;
	
	private GameLevel _gamelevel;
	private GameMap _map;
	
	int NowX;
	int NowY;
	
	public MapMainCharacter(GameLevel gamelevel,int X,int Y)
	{
		_gamelevel = gamelevel;
		sprite = new AnimatedSprite(0,0,gamelevel._resoruceManager.IntelligentLoadTiledTextureRegion("character1.png"));		
		sprite.setScale(2);
		
		
		//SetAt(X,Y);
				
	}
	
	public void MoveTo(int X,int Y)
	{
		Log.d("gameloop","moveTo");
		TargetX = X;
		TargetY = Y;
		
		if(moving)return;		
		TryToMoveTo(TargetX,TargetY);
	}
	
	private boolean stop = false;
	
	public void StopMove()
	{
		stop = true;
	}
	
	private void TryToMoveTo(int X,int Y)
	{
		if(stop)
		{
			sprite.stopAnimation();
			stop = false;
			return;
		}
		
		_map.UpdateAStarTable(X, Y);
		
		
		//every move
		
		
		Log.d("gameloop","_map._AStarTable[NowY][NowX]"+_map._AStarTable[NowY][NowX]);

		if(_map._AStarTable[NowY][NowX]==0)
		{
			sprite.stopAnimation();
			
			
			return;
		}
		
		int min = _map._AStarTable[NowY][NowX];
		int nextX = 0;
		int nextY = 0;
		
		boolean no = false;
		
		if(_map.isAllowedXY(NowX+1, NowY)&&_map._AStarTable[NowY][NowX+1]<min)
		{
			sprite.animate(new long[]{200/3,200/3,200/3},new int[]{3,4,5},1000);
			min = _map._AStarTable[NowY][NowX+1];
			nextX = NowX+1;
			nextY =NowY;
			no =true;
		}
		
		
		if(_map.isAllowedXY(NowX-1, NowY)&&_map._AStarTable[NowY][NowX-1]<min)
		{
			sprite.animate(new long[]{200/3,200/3,200/3},new int[]{9,10,11},1000);
			min = _map._AStarTable[NowY][NowX-1];
			nextX = NowX-1;
			nextY =NowY;
			no =true;
		}
		
		if(_map.isAllowedXY(NowX, NowY+1)&&_map._AStarTable[NowY+1][NowX]<min)
		{
			sprite.animate(new long[]{200/3,200/3,200/3},new int[]{6,7,8},1000);
			min = _map._AStarTable[NowY+1][NowX];
			nextX = NowX;
			nextY =NowY+1;
			no =true;
		}
		
		if(_map.isAllowedXY(NowX, NowY-1)&&_map._AStarTable[NowY-1][NowX]<min)
		{
			sprite.animate(new long[]{200/3,200/3,200/3},new int[]{0,1,2},1000);
			min = _map._AStarTable[NowY-1][NowX];
			nextX = NowX;
			nextY =NowY-1;
			no =true;
		}
		
		if(!no)
			{
			sprite.stopAnimation();
			return;
			}
		
		Log.d("gameloop","Next"+nextX+":"+nextY);
		
	
		
		MoveModifier m = new MoveModifier(0.2f,sprite.getX(),_map._sprite[nextY][nextX].getX(),sprite.getY(),_map._sprite[nextY][nextX].getY()-32);
		sprite.registerEntityModifier(m);
		m.addModifierListener(new IModifierListener(){

			@Override
			public void onModifierStarted(IModifier pModifier, Object pItem) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onModifierFinished(IModifier pModifier, Object pItem) {
				// TODO Auto-generated method stub
				moving = false;
				NowX = tempX;
				NowY = tempY;

				if(_characterMoveListener!=null)
				{
					_characterMoveListener.MovedEvent(NowX, NowY);
				}
				
				TryToMoveTo(TargetX,TargetY);
			}});
		
		
		
		moving = true;
		_map._blockMap[NowY][NowX]=false;
		_map._blockMap[nextY][nextX]=true;		
		
		tempX = nextX;
		tempY = nextY;
	}
	
	int tempX;
	int tempY;
	
	public interface CharacterMoveListener
	{
		public void MovedEvent(int X,int Y);
	}
	public CharacterMoveListener _characterMoveListener ;
	
	public void SetAt(int X,int Y)
	{
		if(!_map.isAllowedXY(X, Y))return;
		
		_map._blockMap[Y][X] = true;
		NowX = X;
		NowY = Y;
		
		sprite.setPosition(_map._sprite[Y][X].getX(),_map._sprite[Y][X].getY()-32);		
		
	}
	
	public void AttachToScene()
	{
		_gamelevel._sceneManager.attach(sprite);
	}
	
	int TargetX;
	int TargetY;
	boolean moving = false;

	public void setMap(GameMap levelMap) {
		// TODO Auto-generated method stub
		_map = levelMap;
	}
	
	
}
